//
//  MainViewController.swift
//  上海微博
//
//  Created by zhangliming on 16/2/22.
//  Copyright © 2016年 zhangliming. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

//    lazy var composeButton:UIButton = {
//
//        composeButton()
//
//    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        ///添加所有控制器

        addControllers()




    }

    //MARK:-加载自控制器方法模型

    private func addChildViewControllers(vc: UIViewController, title: String, imageName: String) {

        vc.title = title

        vc.tabBarItem.image = UIImage(named: imageName)

        //设置tabar字体大小和颜色

        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(15)], forState: .Normal)

        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)


        //设置选中的图片及渲染模式

        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)

        let nav = UINavigationController(rootViewController: vc)

        addChildViewController(nav)
    }

    //MARK:-加载所有控制器方法

    func addControllers(){

        addChildViewControllers(HomeController(), title: "首页", imageName: "tabbar_home")

        addChildViewControllers(MessageController(), title: "消息", imageName: "tabbar_message_center")

        ///添加加号按钮占位空间

        addChildViewController(UIViewController())
        


        addChildViewControllers(DiscoverController(), title: "发现", imageName: "tabbar_discover")

        addChildViewControllers(PriortyController(), title: "我", imageName: "tabbar_profile")


    }

    //MARK:-撰写加号按钮

    lazy var composeButton:UIButton = {

         //设置按钮图片点击事件及图片

        let composeButton = UIButton.zh_UIButton(imageName: "tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")

        //设置点击事件

        composeButton.addTarget(self, action:"didClickComposeButton" , forControlEvents: .TouchUpInside)


        return composeButton

    }()

    ///设置, 添加composeButton

    override func viewWillAppear(animated: Bool) {

        super.viewWillAppear(true)

        ///设置按钮在父控件中的位置

        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)-1

        composeButton.frame = CGRectInset(tabBar.bounds, width * 2, 0)

         tabBar.addSubview(composeButton)

        ///将按钮设置到当前图层

        tabBar.bringSubviewToFront(composeButton)

        ///设置tabar属性

        tabBar.shadowImage = UIImage.zh_siglePoint_image(UIColor(white: 0.9, alpha: 1.0))

        tabBar.backgroundImage = UIImage(named: "tabbar_background")

    }

    //MARK:-监听composeButton

   @objc private func didClickComposeButton(){

        print("点击composeButton")

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    


}
